package com.mobimagic.lockscreen.view;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.mobimagic.lockscreen.AppEnv;
import com.mobimagic.lockscreen.activity.MobileChargingActivity;
import com.mobimagic.lockscreen.sdk.ILockScreenBooster;
import com.mobimagic.lockscreen.sdk.LockScreenSDK;
import com.mobimagic.lockscreen.util.ColorUtil;
import com.nineoldandroids.animation.ObjectAnimator;
import com.qihoo360.common.utils.Utils;

public class MobileChargeColorHelper extends Observable {
    private static final int BOOST_PERCENT = 85;
    private static final int SLIDE_VIEW_COLOR_RED = 0xffffbbb8;
    private static final int SLIDE_VIEW_COLOR_YELLOW = 0xfff9e71a;
    private static final int SLIDE_VIEW_COLOR_BLUE = 0xff84ffff;

    private static final int BG_VIEW_COLOR_RED = 0xfffdbeac;
    private static final int BG_VIEW_COLOR_YELLOW = 0xfffadcc3;
    private static final int BG_VIEW_COLOR_BLUE = 0xffa1c1f6;

    private static final int PRECENT_TEXT_COLOR_RED = 0xffff3d00;
    private static final int PRECENT_TEXT_COLOR_YELLOW = 0xffff9100;
    private static final int PRECENT_TEXT_COLOR_BLUE = 0xff2979ff;

    private static int[] WAVE_COLORS_RED = new int[] { 0x99e63801, 0x99ff3d00 };
    private static int[] WAVE_COLORS_YELLOW = new int[] { 0x99ff9406, 0x99ffa22c };
    private static int[] WAVE_COLORS_BLUE = new int[] { 0x99488dfb, 0x99307dfb };

    private static final int[] BOTTOM_FADE_COLORS_RED = new int[] { 0x00ff4a13, 0xffff4a13, 0xffff4a13 };
    private static final int[] BOTTOM_FADE_COLORS_YELLOW = new int[] { 0x00ffa838, 0xffffa838, 0xffffa838 };
    private static final int[] BOTTOM_FADE_COLORS_BLUE = new int[] { 0x004a8dfb, 0xff4a8dfb, 0xff4a8dfb };

    private static final int[] CHA_PRO_COLOR_RED = new int[] { 0XFFFF4A13, 0XFFFEAF96 };
    private static final int[] CHA_PRO_COLOR_YELLOW = new int[] { 0XFFf39418, 0X66ffffff };
    private static final int[] CHA_PRO_COLOR_BLUE = new int[] { 0XFF4289fb, 0X66ffffff };

    private boolean mBoosting;
    private static final boolean DEBUG = AppEnv.bAppdebug;
    private static final String TAG = "MobileChargeColorHelper";

    private ColorTheme colorTheme;
    private Handler mHandler;
    private ChargeColor mChargeColor;
    private ILockScreenBooster mLockScreenBooster;

    public enum ColorTheme {
        Red, Yellow, Blue;
    }

    public ColorTheme getColorTheme() {
        return colorTheme;
    }

    public void setColorTheme(ColorTheme colorTheme) {
        this.colorTheme = colorTheme;
        if (DEBUG) {
            Log.d(TAG, "setColorTheme.colorTheme=" + colorTheme);
        }
    }

    public MobileChargeColorHelper(Context context) {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                MobileChargeColorHelper.this.handleMessage(msg);
            }
        };
        mObjectAnimator = ColorUtil.getArgbEvaluatorObjectAnimator();
        mLockScreenBooster = LockScreenSDK.getInstance().getLockScreenBooster();
        mChargeColor = new ChargeColor();
        setColorTheme(ColorTheme.Blue);
        setChargeColorData();
    }

    /**
     * 内存是否大于等于85%
     */
    public boolean isMemoryMeet() {
        if (DEBUG) {
            Log.d(TAG, "isMemoryMeet.memery=" + Utils.getMemoryUsedPercent() + "上限为" + BOOST_PERCENT);
        }

        return Utils.getMemoryUsedPercent() >= BOOST_PERCENT;
    }

    public void setColorThemeBlue() {
        ColorTheme colorTheme = getColorTheme();
        if (DEBUG) {
            Log.d(TAG, "setColorThemeBlue.colorTheme=" + colorTheme);
        }
        setBoostStatus(false);
        if (colorTheme == null || colorTheme != ColorTheme.Blue) {
            setColorTheme(ColorTheme.Blue);
        }
    }

    public void setBoosting(boolean boosting) {
        mBoosting = boosting;
    }

    public boolean isBoostring() {
        return mBoosting;
    }

    public void setScreenOnColorTheme() {
        boolean isprotect = mLockScreenBooster.isBoostProtectionPeriod();
        boolean isMeet = isMemoryMeet();
        if (!isprotect) {
            setBoostStatus(false);
            setColorTheme(ColorTheme.Blue);
        } else {
            if (isMeet) {
                mLockScreenBooster.forceLoadBoostData();
                setBoostStatus(true);
                setColorTheme(ColorTheme.Red);
            } else {
                setBoostStatus(false);
                setColorTheme(ColorTheme.Blue);
            }
        }
        if (DEBUG) {
            Log.d(TAG, "setScreenOnColorTheme.isprotect=" + isprotect + " isMeet=" + isMeet);
        }

    }

    public boolean needDiscolorationBoost() {
        return isExcuDiscolorBoost;
    }

    public void setBoostStatus(boolean need) {
        isExcuDiscolorBoost = need;
    }

    public void setConnectTheme() {
        boolean isProtectionPreriod = mLockScreenBooster.isBoostProtectionPeriod();
        if (!isProtectionPreriod) {
            setBoostStatus(false);
            setColorTheme(ColorTheme.Blue);
        } else {
            mLockScreenBooster.forceLoadBoostData();
            setBoostStatus(true);
            if (isMemoryMeet()) {
                setColorTheme(ColorTheme.Red);
            } else {
                setColorTheme(ColorTheme.Yellow);
            }
        }
        if (DEBUG) {
            Log.i(TAG, "setConnectTheme==ColorTheme" + getColorTheme() + " isProtectionPreriod" + isProtectionPreriod);
        }
    }

    public int getSlideTextViewColor() {
        int color = 0;
        ColorTheme colorTheme = getColorTheme();
        switch (colorTheme) {
        case Red:
            color = SLIDE_VIEW_COLOR_RED;
            break;
        case Yellow:
            color = SLIDE_VIEW_COLOR_YELLOW;
            break;
        case Blue:
            color = SLIDE_VIEW_COLOR_BLUE;
            break;
        default:
            color = SLIDE_VIEW_COLOR_BLUE;
            break;
        }
        return color;
    }

    public int getBGViewColor() {
        int color = 0;
        ColorTheme colorTheme = getColorTheme();
        switch (colorTheme) {
        case Red:
            color = BG_VIEW_COLOR_RED;
            break;
        case Yellow:
            color = BG_VIEW_COLOR_YELLOW;
            break;
        case Blue:
            color = BG_VIEW_COLOR_BLUE;
            break;
        default:
            color = BG_VIEW_COLOR_BLUE;
            break;
        }
        return color;
    }

    public int[] getWaveColor() {
        ColorTheme colorTheme = getColorTheme();
        int[] colors = null;
        switch (colorTheme) {
        case Yellow:
            colors = WAVE_COLORS_YELLOW;
            break;
        case Red:
            colors = WAVE_COLORS_RED;
            break;
        case Blue:
            colors = WAVE_COLORS_BLUE;
            break;

        default:
            colors = WAVE_COLORS_BLUE;
            break;
        }
        return colors;
    }

    public int[] getBottomFadeColors() {
        ColorTheme colorTheme = getColorTheme();
        int[] colors = null;
        switch (colorTheme) {
        case Yellow:
            colors = BOTTOM_FADE_COLORS_YELLOW;
            break;
        case Red:
            colors = BOTTOM_FADE_COLORS_RED;
            break;
        case Blue:
            colors = BOTTOM_FADE_COLORS_BLUE;
            break;

        default:
            colors = BOTTOM_FADE_COLORS_BLUE;
            break;
        }
        return colors;
    }

    public int getChargingPercentTextColor() {
        int color = 0;
        ColorTheme colorTheme = getColorTheme();
        switch (colorTheme) {
        case Red:
            color = PRECENT_TEXT_COLOR_RED;
            break;
        case Yellow:
            color = PRECENT_TEXT_COLOR_YELLOW;
            break;
        case Blue:
            color = PRECENT_TEXT_COLOR_BLUE;
            break;
        default:
            color = PRECENT_TEXT_COLOR_BLUE;
            break;
        }
        return color;
    }

    public int[] getChaProColor() {
        int[] colors = null;
        ColorTheme colorTheme = getColorTheme();
        switch (colorTheme) {
        case Red:
            colors = CHA_PRO_COLOR_RED;
            break;
        case Yellow:
            colors = CHA_PRO_COLOR_YELLOW;
            break;
        case Blue:
            colors = CHA_PRO_COLOR_BLUE;
            break;
        default:
            colors = CHA_PRO_COLOR_BLUE;
            break;
        }
        return colors;
    }

    private final int COLOR_TO_COLOR = 1;
    private int colorPro;
    private boolean isExcuDiscolorBoost;
    private ObjectAnimator mObjectAnimator;

    public void startDiscoloration() {
        ColorTheme colorTheme = getColorTheme();

        if (DEBUG) {
            Log.d(TAG, "startDiscoloration.getColorTheme() " + colorTheme);
        }

        if (colorTheme != null && colorTheme != ColorTheme.Blue) {
            setChargeColorData();
            setAnimColor();
        }

    }

    public void setChargeColorData() {
        if (mChargeColor != null) {
            mChargeColor.setSlideViewStartColor(getSlideTextViewColor());
            mChargeColor.setBgViewStartColor(getBGViewColor());
            mChargeColor.setPrecentTextStartColor(getChargingPercentTextColor());
            int[] proColors = getChaProColor();
            int[] bottomColors = getBottomFadeColors();
            int[] waveColors = getWaveColor();
            mChargeColor.setCircleProInnerStartColor(proColors[0]);
            mChargeColor.setSpeedLineStartColor(proColors[1]);
            mChargeColor.setBottomFadeStartColor1(bottomColors[0]);
            mChargeColor.setBottomFadeStartColor2(bottomColors[1]);
            mChargeColor.setBottomFadeStartColor3(bottomColors[2]);
            mChargeColor.setWaveStartColor1(waveColors[0]);
            mChargeColor.setWaveStartColor2(waveColors[1]);

            mChargeColor.setSlideViewCurrentColor(getSlideTextViewColor());
            mChargeColor.setBgViewCurrentColor(getBGViewColor());
            mChargeColor.setPrecentTextCurrentColor(getChargingPercentTextColor());
            mChargeColor.setCircleProInnerCurrentColor(proColors[0]);
            mChargeColor.setSpeedLineCurrentColor(proColors[1]);
            mChargeColor.setBottomFadeCurrentColor1(bottomColors[0]);
            mChargeColor.setBottomFadeCurrentColor2(bottomColors[1]);
            mChargeColor.setBottomFadeCurrentColor3(bottomColors[2]);
            mChargeColor.setWaveCurrentColor1(waveColors[0]);
            mChargeColor.setWaveCurrentColor2(waveColors[1]);

            mChargeColor.setSlideViewEndColor(SLIDE_VIEW_COLOR_BLUE);
            mChargeColor.setBgViewEndColor(BG_VIEW_COLOR_BLUE);
            mChargeColor.setPrecentTextEndColor(PRECENT_TEXT_COLOR_BLUE);
            mChargeColor.setCircleProInnerEndColor(CHA_PRO_COLOR_BLUE[0]);
            mChargeColor.setSpeedLineEndColor(CHA_PRO_COLOR_BLUE[1]);
            mChargeColor.setBottomFadeEndColor1(BOTTOM_FADE_COLORS_BLUE[0]);
            mChargeColor.setBottomFadeEndColor2(BOTTOM_FADE_COLORS_BLUE[1]);
            mChargeColor.setBottomFadeEndColor3(BOTTOM_FADE_COLORS_BLUE[2]);
            mChargeColor.setWaveEndColor1(WAVE_COLORS_BLUE[0]);
            mChargeColor.setWaveEndColor2(WAVE_COLORS_BLUE[1]);
            setChanged();
            notifyObservers(mChargeColor);
            if (DEBUG) {
                Log.d(TAG, "setChargeColorData.countObservers=" + countObservers());
            }

        }
    }

    public void setAnimColor() {
        Message message = mHandler.obtainMessage();
        message.what = COLOR_TO_COLOR;
        mHandler.sendMessage(message);
        colorPro = 1;
        if (DEBUG) {
            Log.d(TAG, "setAnimColor");
        }

    }

    public void destroy() {
        mHandler.removeMessages(COLOR_TO_COLOR);
    }

    public void handleMessage(Message msg) {
        switch (msg.what) {
        case COLOR_TO_COLOR:
            getCurrentColor();
            break;
        default:
            break;
        }
        colorPro++;
        setChanged();
        notifyObservers(mChargeColor);
        if (colorPro <= 20) {
            Message message = mHandler.obtainMessage();
            message.what = COLOR_TO_COLOR;
            mHandler.sendMessageDelayed(message, 150);
        } else {
            setChanged();
            notifyObservers(mChargeColor);
        }
    }

    private void getCurrentColor() {
        int curSlideColor = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getSlideViewStartColor(), mChargeColor.getSlideViewEndColor());
        int curbgViewColor = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getBgViewStartColor(), mChargeColor.getBgViewEndColor());
        int curPrecentColor = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getPrecentTextStartColor(), mChargeColor.getPrecentTextEndColor());
        int curWave1Color = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getWaveStartColor1(), mChargeColor.getWaveEndColor1());
        int curWave2Color = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getWaveStartColor2(), mChargeColor.getWaveEndColor2());
        int curCirProInerColor = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getCircleProInnerStartColor(), mChargeColor.getCircleProInnerEndColor());
        int curSpeedLineColor = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getSpeedLineStartColor(), mChargeColor.getSpeedLineEndColor());
        int curBottFade1Color = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getBottomFadeStartColor1(), mChargeColor.getBottomFadeEndColor1());
        int curBottFade2Color = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getBottomFadeStartColor2(), mChargeColor.getBottomFadeEndColor2());
        int curBottFade3Color = ColorUtil.getColorsValue(mObjectAnimator, colorPro * 0.05f,
                mChargeColor.getBottomFadeStartColor3(), mChargeColor.getBottomFadeEndColor3());
        mChargeColor.setSlideViewCurrentColor(curSlideColor);
        mChargeColor.setBgViewCurrentColor(curbgViewColor);
        mChargeColor.setPrecentTextCurrentColor(curPrecentColor);
        mChargeColor.setWaveCurrentColor1(curWave1Color);
        mChargeColor.setWaveCurrentColor2(curWave2Color);
        mChargeColor.setCircleProInnerCurrentColor(curCirProInerColor);
        mChargeColor.setSpeedLineCurrentColor(curSpeedLineColor);
        mChargeColor.setBottomFadeCurrentColor1(curBottFade1Color);
        mChargeColor.setBottomFadeCurrentColor2(curBottFade2Color);
        mChargeColor.setBottomFadeCurrentColor3(curBottFade3Color);
    }

    @Override
    public void addObserver(Observer observer) {
        super.addObserver(observer);
        if (observer != null) {
            observer.update(this, mChargeColor);
        }
    }

    public void change(int from, boolean refresh) {

        boolean isBoosting = isBoostring();
        if (DEBUG) {
            Log.d(TAG, "change.from=" + from + " refresh=" + refresh + " isBoosting " + isBoosting);
        }

        switch (from) {
        case MobileChargingActivity.EXTRA_FROM_CONNECT:
            setConnectTheme();
            break;
        case MobileChargingActivity.EXTRA_FROM_DISCONNECT:
            setBoostStatus(false);
            //            if (!isBoostring()) {
            //                setColorThemeBlue();
            //            }
            break;
        case MobileChargingActivity.EXTRA_FROM_CLICK_TRIP_BTN:
        case MobileChargingActivity.EXTRA_FROM_BOOST_CARD:
            setConnectTheme();
            break;
        case MobileChargingActivity.EXTRA_FROM_SCREENON:
        case MobileChargingActivity.EXTRA_FROM_OTHER_CHARGING_APP_SCREEN_ON:
            setScreenOnColorTheme();
            break;
        case MobileChargingActivity.EXTRA_FROM_SCREENOFF:
        case MobileChargingActivity.EXTRA_FROM_OTHER_CHARGING_APP_SCREEN_OFF:
            if (!isBoostring()) {
                setColorThemeBlue();
            }
            break;
        case MobileChargingActivity.EXTRA_FROM_ONLY_REFRESH:
            setConnectTheme();
            break;
        default:
            break;
        }

    }

}
